package com.study.d012Components;

import com.entity.Student;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * ChoiceBox 下拉框的使用
 *
 */
public class ChoiceBoxMain extends Application
{
	int index;
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {

		stage.show();

		ChoiceBox<String> cb1 = new ChoiceBox<String>();
		cb1.getItems().addAll("读书","学习","打豆豆");

		//设置宽度
		cb1.setPrefWidth(200);

		AnchorPane aPane  = new AnchorPane();
		aPane.getChildren().add(cb1);
		stage.setScene(new Scene(aPane, 400, 300));

		//设置默认选中
		//cb1.setValue("学习");
		//cb1.getSelectionModel().select("学习");

		//下拉框监听事件
		cb1.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				System.out.println("你选择的是：" + newValue);
			}
		});




		//使用Student泛型
		ChoiceBox<Student> cb2 = new ChoiceBox<Student>();
		Student s1 = new Student("张三", 19);
		Student s2 = new Student("李四", 20);
		Student s3 = new Student("王五", 21);
		cb2.getItems().addAll(s1,s2,s3);
		aPane.getChildren().add(cb2);
		AnchorPane.setTopAnchor(cb2, 40.0);

		//当使用自定义类作为元素时，需要进行转换，当然也可以在Student的toString()中进行格式化输出
		cb2.setConverter(new StringConverter<Student>() {
			@Override
			public String toString(Student object) {
				//在这里进行格式化输出为字符串
				if(object ==null){
					return null;
				}
				return object.getName() + "-" + object.getAge();
			}

			@Override
			public Student fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		});


		//两个下拉框实现类似省市联动的效果
		ChoiceBox<String> cb3 = new ChoiceBox<String>();
		cb3.getItems().addAll("数字","字母");

		ChoiceBox<String> cb4 = new ChoiceBox<String>();
		ObservableList<String>  charList=FXCollections.observableArrayList();
		charList.addAll("A","B","C","D","E");

		ObservableList<String>  numList=FXCollections.observableArrayList();
		numList.addAll("1","2","3","4","5");

		aPane.getChildren().addAll(cb3,cb4);
		AnchorPane.setTopAnchor(cb3, 100.0);
		AnchorPane.setTopAnchor(cb4, 100.0);
		AnchorPane.setLeftAnchor(cb4, 100.0);

		cb3.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				System.out.println(newValue);
				if("数字".equals(newValue)){
					cb4.setItems(numList);
				}else if("字母".equals(newValue)){
					cb4.setItems(charList);
				}
				cb4.show();
			}
		});



		//修改ChoiceBox元素
		ChoiceBox<Student> cb5 = new ChoiceBox<Student>();
		cb5.setPrefWidth(140);
		Student ss1 = new Student("张三", 19);
		Student ss2 = new Student("李四", 20);
		Student ss3 = new Student("王五", 21);

		Label upLabel  =new Label("修改选项:");
		Button upButton = new Button("修改");

		cb5.getItems().addAll(ss1,ss2,ss3);
		aPane.getChildren().addAll(upLabel,cb5,upButton);

		AnchorPane.setTopAnchor(upLabel, 140.0);

		AnchorPane.setTopAnchor(cb5, 140.0);
		AnchorPane.setLeftAnchor(cb5, 70.0);

		AnchorPane.setTopAnchor(upButton, 140.0);
		AnchorPane.setLeftAnchor(upButton, 250.0);

		cb5.setConverter(new StringConverter<Student>() {

			@Override
			public String toString(Student object) {
				if(object !=null){
					return object.getName() + "-" + object.getAge();
				}
				return null;
			}

			@Override
			public Student fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		});


		//获取选中的索引
		cb5.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				index = newValue.intValue();
				System.out.println("你选择的索引是:" + index);
			}
		});

		//修改
		/**
		 * ChoiceBox在修改时，需要先移除，再插入
		 * ComboBox在修改时，需要先情况选中，再重新选中
		 */
		upButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				int temp = index;
				Student s= cb5.getItems().get(index);
				s.setName("你被选中了");
				s.setAge(50);
				cb5.getItems().remove(index);
				cb5.getItems().add(temp, s);
			}
		});

	}
}


